package com.jiai.cloud.sys.controller;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TApplication;
import com.jiai.cloud.entity.TMember;
import com.jiai.cloud.sys.service.ITMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 商品表 前端
 * 控制器
 * </p>
 *
 * @佳爱科技
 * @since 2017-05-24
 */
@RestController
@RequestMapping("/api/v1/member")
public class TMemberController {
    @Autowired
    private ITMemberService itMemberService;

    //增加
    @RequestMapping(value = "/addApplication",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object addApplication(@RequestBody TMember tMember)throws ServiceException {
        try{
            itMemberService.insert(tMember);
            return RestResultGenerator.genSuccessResult("插入成功");
        }catch (Exception e){
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //通过ID删除
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException{
        try{
            itMemberService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }


    //查所有
    @RequestMapping(value = "/findByAll/{page}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll(@PathVariable("page") Integer page) throws ServiceException{
        try {
            Page<TMember> p= new Page<>(page, 20);
            itMemberService.selectPage(p, null);
            return new CustomPage(p);
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //保存和更新
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(@RequestBody TMember tMember)throws ServiceException{
        try {
            itMemberService.insertOrUpdate(tMember);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }

}
